package com.oa.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.oa.android.R;
import com.oa.android.utils.SettingUtil;

import java.util.List;

/**
 * 圈子
 * Created by zouqh on 2018/4/1.
 */

public class SendrecyclerAdapter extends RecyclerView.Adapter<SendrecyclerAdapter.BaseViewHolder> {
    private static final int ITEM_IMAGE = 1;
    private static final int ITEM_IMAGES = 2;

    private List<String> datas;
    private Context mContext;
    private int viewFooter;
    private SendrecyclerAdapter.OnItemClickListener mOnItemClickListener;
    private SendrecyclerAdapter.OnLongItemClickListener longItemClickListener;
    private int itemWidth;
    private int typeItem;

    public SendrecyclerAdapter(List<String> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
        itemWidth = (SettingUtil.getScreenWidth(context) - SettingUtil.dip2px(context, 32)) / 3;
    }

    public void replaceAll(List<String> elements) {
        if (datas.size() > 0) {
            datas.clear();
        }
        datas.addAll(elements);
        notifyDataSetChanged();
    }

    public void addAll(String elements) {
        datas.add(elements);
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_IMAGE) {
            return new ImageHolder(LayoutInflater.from(mContext).inflate(R.layout.select_img_img, parent, false));
        } else {
            return new AddImgHolder(LayoutInflater.from(mContext).inflate(R.layout.select_img_img, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {

        if (holder instanceof ImageHolder) {
            setItemImage((ImageHolder) holder, position);
        } else if (holder instanceof AddImgHolder) {
            setItemAddImage((AddImgHolder) holder, position);
        }


    }

    private void setItemImage(final ImageHolder holder, final int position){
        Glide.with(mContext).load(datas.get(position)).centerCrop().into(holder.imageview);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                longItemClickListener.OnLongItemClick(position, holder);
                return true;
            }
        });
    }

    private void setItemAddImage(final AddImgHolder holder, final int position){
        holder.imageview2.setImageResource(R.mipmap.add_to_icon_03);
        if (datas.size() >= 10) {//集合长度大于等于9张时，隐藏 图片
            holder.imageview2.setVisibility(View.GONE);
        }else{
            holder.imageview2.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(position, holder);
            }
        });
    }

    @Override
    public int getItemCount() {
//        int count = (datas == null ? 0 : datas.size());
//        if (viewFooter != 0) {
//            count++;
//        }
        return datas.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return ITEM_IMAGES;
        } else {
            return ITEM_IMAGE;
        }
    }

    public void addFooterView(int footerView) {
        this.viewFooter = footerView;
        notifyItemInserted(getItemCount() - 1);
    }


    class ImageHolder extends BaseViewHolder {
        private final ImageView imageview;
        public ImageHolder(View itemView) {
            super(itemView);
            imageview = (ImageView) itemView.findViewById(R.id.image);
        }
    }

    class AddImgHolder extends BaseViewHolder {
        private final ImageView imageview2;
        public AddImgHolder(View itemView) {
            super(itemView);
            imageview2 = (ImageView) itemView.findViewById(R.id.image);
        }
    }


    //设置点击事件
    public void setOnItemClickLitener(SendrecyclerAdapter.OnItemClickListener mLitener) {
        mOnItemClickListener = mLitener;
    }
    public void setOnLongItemClickLitener(SendrecyclerAdapter.OnLongItemClickListener mLitener){
        longItemClickListener = mLitener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, BaseViewHolder viewHolder);
    }
    public interface OnLongItemClickListener {
        void OnLongItemClick(int position, BaseViewHolder viewHolder);
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {


        public BaseViewHolder(View view) {
            super(view);

        }

    }

}